﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Framework;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI.WebControls;

    public class Clients : UserControlBase
    {
        [AccessedThroughProperty("cmdSearch")]
        private LinkButton _cmdSearch;
        [AccessedThroughProperty("dgUserList")]
        private DataGrid _dgUserList;
        private string _Filter;
        private int _PortalID;
        private string _ResourceFile;
        [AccessedThroughProperty("txtSearch")]
        private TextBox _txtSearch;

        public event SelectedUserEventHandler SelectedUser;

        public Clients()
        {
            base.Load += new EventHandler(this.Page_Load);
            this._Filter = "";
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            this.dgUserList.CurrentPageIndex = 0;
            this.populateUserList();
        }

        private void dgUserList_EditCommand(object source, DataGridCommandEventArgs e)
        {
            int userID = int.Parse(e.CommandArgument.ToString());
            
            if (this.SelectedUser != null)
            {
                SelectedUser(userID);
            }
        }

        private void dgUserList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgUserList.CurrentPageIndex = e.NewPageIndex;
            this.populateUserList();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.populateUserList();
            }
        }

        private void populateUserList()
        {
            ClientController controller = new ClientController();
            DataGrid dgUserList = this.dgUserList;
            DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(ref dgUserList, this._ResourceFile);
            this.dgUserList = dgUserList;
            ArrayList users = controller.GetUsers(this._PortalID, this.txtSearch.Text);
            this.dgUserList.DataSource = users;
            this.dgUserList.DataBind();
        }

        protected virtual LinkButton cmdSearch
        {
            get
            {
                return this._cmdSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                EventHandler handler = new EventHandler(this.cmdSearch_Click);
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click -= handler;
                }
                this._cmdSearch = value;
                if (this._cmdSearch != null)
                {
                    this._cmdSearch.Click += handler;
                }
            }
        }

        protected virtual DataGrid dgUserList
        {
            get
            {
                return this._dgUserList;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataGridCommandEventHandler handler = new DataGridCommandEventHandler(this.dgUserList_EditCommand);
                DataGridPageChangedEventHandler handler2 = new DataGridPageChangedEventHandler(this.dgUserList_PageIndexChanged);
                if (this._dgUserList != null)
                {
                    this._dgUserList.EditCommand -= handler;
                    this._dgUserList.PageIndexChanged -= handler2;
                }
                this._dgUserList = value;
                if (this._dgUserList != null)
                {
                    this._dgUserList.EditCommand += handler;
                    this._dgUserList.PageIndexChanged += handler2;
                }
            }
        }

        public string PortalID
        {
            get
            {
                return Conversions.ToString(this._PortalID);
            }
            set
            {
                this._PortalID = Conversions.ToInteger(value);
            }
        }

        public string ResourceFile
        {
            get
            {
                return this._ResourceFile;
            }
            set
            {
                this._ResourceFile = value;
            }
        }

        protected virtual TextBox txtSearch
        {
            get
            {
                return this._txtSearch;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._txtSearch = value;
            }
        }

        public delegate void SelectedUserEventHandler(int UserID);
    }
}

